# 阿里
import pandas as pd
import fractions
import re
import os

def rename(path):

    FileList = os.listdir(path)
    for files in FileList:

        oldDirPath = os.path.join(path, files)

        if os.path.isdir(oldDirPath):
            rename(oldDirPath)

        fileName = os.path.splitext(files)[0]
        fileName = fileName.split(' ')[0]

        fileType = os.path.splitext(files)[1]

        newDirPath = os.path.join(path, fileName + fileType)

        os.rename(oldDirPath, newDirPath)

    try:
        # print("阿里云价格表重命名")
        csv = pd.read_csv('./testPro/output/instancePrice.csv', encoding='utf-8')
        csv.to_excel('./testPro/output/ali-price.xlsx', sheet_name='data',index=False)
    except:
        pass

    try:
        # print("腾讯云价格表重命名")
        csv = pd.read_csv('./testPro/output/cvm-price.csv', encoding='utf-8')
        csv.to_excel('./testPro/output/tcnt-price.xlsx', sheet_name='data',index=False)
    except:
        pass

    try:
        os.remove('./testPro/output/instancePrice.csv')
    except:
        pass

    try:
        os.remove('./testPro/output/cvm-price.csv')
    except:
        pass

rename('./testPro/output/')

# print("阿里云 各实例规格族配比计算中...")

def formatProportion(proporthion, split='; '):
    temp = re.findall(r'\d+\:\d+', proporthion)
    res = ''

    for i in temp:
        res += i+split
    
    return res[:-2]

def aliTableUnion(path1,path2):
    # region ====ali====
    # 开文件
    small_sheet_ali = pd.read_excel(path1, engine='openpyxl',sheet_name=0)
    big_sheet_ali = pd.read_excel(path2, engine='openpyxl',sheet_name=0)

    def aliSplit(s):
        s = s.split('.')[1].split('-')
        if len(s) > 1:
            res = s[0]
            for i in range(1,len(s)):
                if i < len(s)-1:
                    res += '-'+s[i]
            s = res
        else:
            s = s[0]
        return s
    # 建立一个字典
    alidict = dict()

    for l,s in big_sheet_ali['Instance_specification'].items():
        try:
            s = aliSplit(s)
            if s == "video":
                s = "video-trans"
            if s == "ebmre6":
                s = "ebmre6-6t"
            if s in alidict.keys():
                try:
                    a = fractions.Fraction(int(float(big_sheet_ali.loc[l,'vCpu'])*10),int(float(big_sheet_ali.loc[l,'Memory_GiB'])*10))
                except:
                    print(int(big_sheet_ali.loc[l,'vCpu']),int(big_sheet_ali.loc[l,'Memory_GiB']),s,l)
                temp = str(a.numerator) + ':' + str(a.denominator)
                if temp in alidict[s]:
                    pass
                else:
                    alidict[s] += temp+' '
            else:
                try:
                    a = fractions.Fraction(int(float(big_sheet_ali.loc[l,'vCpu'])*10),int(float(big_sheet_ali.loc[l,'Memory_GiB'])*10))
                except:
                    print(int(big_sheet_ali.loc[l,'vCpu']),int(big_sheet_ali.loc[l,'Memory_GiB']),s,l)
                temp = str(a.numerator) + ':' + str(a.denominator)
                alidict.update({s:temp+' '})
        except:
            pass
    for l,s in small_sheet_ali['Family'].items():
        if s in alidict.keys():
            small_sheet_ali.loc[l, 'Proportion'] = formatProportion(alidict[s])
    small_sheet_ali.to_excel('./testPro/output/ali-family.xlsx',index=False)
    # endregion

def baiTableUnion(path1,path2):
    # print("百度云 各实例规格族配比计算中...")
    # region ===bai===
    small_sheet_bai = pd.read_excel(path1, engine='openpyxl',sheet_name=0)
    big_sheet_bai = pd.read_excel(path2, engine='openpyxl',sheet_name=0)

    # 建立一个字典
    baidict = dict()
    for l,s in big_sheet_bai['Instance_specification'].items():
        s = s.split('.')[1] # 百度的分离公式
        if s in baidict.keys():
            try:
                a = fractions.Fraction(int(float(big_sheet_bai.loc[l,'vCpu'])*10),int(float(big_sheet_bai.loc[l,'Memory_GiB'])*10))
                #print(int(big_sheet_bai.loc[l,'vCpu']),int(big_sheet_bai.loc[l,'memory']),s,l)
            except:
                print(int(big_sheet_bai.loc[l,'vCpu']),int(big_sheet_bai.loc[l,'Memory_GiB']),s,l)
            temp = str(a.numerator) + ':' + str(a.denominator)
            if temp in baidict[s]:
                pass
            else:
                baidict[s] += temp+' '
        else:
            try:
                a = fractions.Fraction(int(float(big_sheet_bai.loc[l,'vCpu'])*10),int(float(big_sheet_bai.loc[l,'Memory_GiB'])*10))
            except:
                print(int(big_sheet_bai.loc[l,'vCpu']),int(big_sheet_bai.loc[l,'Memory_GiB']),s,l)
            temp = str(a.numerator) + ':' + str(a.denominator)
            baidict.update({s:temp+' '})

    for l,s in small_sheet_bai['Family'].items():
        if s in baidict.keys():
            small_sheet_bai.loc[l, 'Proportion'] = formatProportion(baidict[s])
    small_sheet_bai.to_excel('./testPro/output/bai-family.xlsx',index=False)
    # endregion

def tcntTableUnion(path1,path2):
    # print("腾讯云 各实例规格族配比计算中...")
    # region ===tenc===
    small_sheet_tenc = pd.read_excel(path1, engine='openpyxl',sheet_name=0)
    big_sheet_tenc = pd.read_excel(path2, engine='openpyxl',sheet_name=0)

    # 建立一个字典
    tcntdict = dict()
    for l,s in big_sheet_tenc['Instance_specification'].items():
        try:
            s = s.split('.')[0] # 百度的分离公式
            if s in tcntdict.keys():
                try:
                    a = fractions.Fraction(int(float(big_sheet_tenc.loc[l,'vCpu'])*10),int(float(big_sheet_tenc.loc[l,'Memory_GiB'])*10))
                except:
                    print(int(big_sheet_tenc.loc[l,'vCpu']),int(big_sheet_tenc.loc[l,'Memory_GiB']),s,l)
                temp = str(a.numerator) + ':' + str(a.denominator)
                if temp in tcntdict[s]:
                    pass
                else:
                    tcntdict[s] += temp+' '
            else:
                try:
                    a = fractions.Fraction(int(float(big_sheet_tenc.loc[l,'vCpu'])*10),int(float(big_sheet_tenc.loc[l,'Memory_GiB'])*10))
                except:
                    print(int(big_sheet_tenc.loc[l,'vCpu']),int(big_sheet_tenc.loc[l,'Memory_GiB']),s,l)
                temp = str(a.numerator) + ':' + str(a.denominator)
                tcntdict.update({s:temp+' '})
        except:
            continue

    for l,s in small_sheet_tenc['Family'].items():
        if s in tcntdict.keys():
            small_sheet_tenc.loc[l, 'Proportion'] = formatProportion(tcntdict[s])
    small_sheet_tenc.to_excel('./testPro/output/tcnt-family.xlsx',index=False)
    # endregion

aliTableUnion('./testPro/output/ali-family.xlsx','./testPro/output/ali-family-details.xlsx')
baiTableUnion('./testPro/output/bai-family.xlsx','./testPro/output/bai-family-details.xlsx')
tcntTableUnion('./testPro/output/tcnt-family.xlsx','./testPro/output/tcnt-family-details.xlsx')